package com.shycloud.mido.homework.pattern;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.common.core.util.SpringContextHolder;
import com.shycloud.mido.homework.api.vo.HomeworkDashboardReqVO;
import com.shycloud.mido.homework.api.vo.HomeworkDashboardRespVO;
import com.shycloud.mido.homework.constant.Constant;
import com.shycloud.mido.homework.mapper.MultiTeacherClassroomLinkMapper;
import com.shycloud.mido.member.api.entity.MultiTeacherClassroomLink;

@Component(Constant.TEA_MULTI_STU_ONE_RULE)
@Order(5)
public class TeaMultiStuOneRule extends BaseHomeworkDatas implements IHomeworkDashboardRule {
	
	@Override
	public R<Void> getViewDatas(LocalDate startDate,HomeworkDashboardReqVO vo,Map<LocalDate, HomeworkDashboardRespVO> resultMap) {
		//规则入参校验
		boolean studentFlag = vo.isStudentFlag();//false
		String mode = vo.getMode();//1
		String isParentMode = vo.getIsParentMode();//0
		String teacherId = vo.getTeacherId();//is not null
		if (BooleanUtils.isFalse(studentFlag) && StringUtils.equals("1", mode) && StringUtils.equals("0", isParentMode) && StringUtils.isNotBlank(teacherId)) {
			HomeworkDashboardRespVO mapBean = new HomeworkDashboardRespVO();
			resultMap.put(startDate, mapBean);
			
			String classroomId = vo.getClassroomId();
			String studentId = vo.getStudentId();
			if (StringUtils.isEmpty(studentId)) {
				throw new BusinessException("学生ID不能为空");
			}
			//查询教室中的全部老师
			MultiTeacherClassroomLinkMapper multiTeacherClassroomLinkMapper = SpringContextHolder.getBean(MultiTeacherClassroomLinkMapper.class);
	        
			LambdaQueryWrapper<MultiTeacherClassroomLink> multiTeacherClassroomLinkLQW = Wrappers.<MultiTeacherClassroomLink>lambdaQuery()
					.eq(MultiTeacherClassroomLink::getClassroomId, classroomId)
					.eq(MultiTeacherClassroomLink::getTeacherId, teacherId)
					.eq(MultiTeacherClassroomLink::getDelFlag, Constant.DEL_FLAG);
			List<MultiTeacherClassroomLink> multiTeacherClassroomLinkList = multiTeacherClassroomLinkMapper.selectList(multiTeacherClassroomLinkLQW);
			if (CollectionUtils.isNotEmpty(multiTeacherClassroomLinkList)) {
				List<String> teacherIds = multiTeacherClassroomLinkList.stream().map(MultiTeacherClassroomLink::getTeacherId).collect(Collectors.toList());
				
				super.getAllDatas(startDate, mapBean, studentId,teacherIds);
			}else {
				throw new BusinessException("该班级没有该老师");
			}
			
		}else {
			throw new BusinessException("参数异常");
		}
		return new R<Void>();
	}
	
    
}